<template>
  <el-form
    ref="dialogForm"
    :model="dialogForm"
    label-width="80px"
    :rules="dialogRules" size="small"
  >
    <el-form-item label="姓名" prop="userName">
      <el-input v-model="dialogForm.userName" clearable />
    </el-form-item>
    <el-form-item label="登录名" prop="loginName">
      <el-input clearable
        v-model="dialogForm.loginName"
        :disabled="dialogForm.id ? true : false"
      />
    </el-form-item>
    <el-form-item v-if="!dialogForm.id" label="登录密码" prop="password">
      <el-input v-model.trim="dialogForm.password" @input="dialogForm.password = dialogForm.password.replace(/[\u4E00-\u9FA5]/g,'')" clearable />
    </el-form-item>
    <el-form-item label="手机号">
      <el-input v-model="dialogForm.phone" clearable />
    </el-form-item>
    <el-form-item label="组织机构" prop="organizationId">
      <el-cascader 
        :disabled="!dialogForm.id"
        style="width: 100%"
        :options="orgOptions"
        ref="orgRef"
        :props="defaultProps"
        clearable
        @change="orgChange"
        v-model="dialogForm.organizationId"
        placeholder="请选择组织机构"
      ></el-cascader>
    </el-form-item>
    <el-form-item label="角色" prop="roleIds">
      <el-select v-model="dialogForm.roleIds" multiple clearable placeholder="请选择角色">
        <!-- 厂商管理员&& 平台用户不可编辑 -->
        <el-option
          :disabled="
            item.id == 1 || (item.id == 2 && currentChooseOrgType == 'P')
          "
          v-for="item in options"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        >
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item class="operate-btns">
      <el-button size="small" @click="closeModel">取消</el-button>
      <el-button
        size="small"
        :loading="loading"
        type="primary"
        @click="onSubmit('dialogForm')"
        >确认</el-button
      >
    </el-form-item>
  </el-form>
</template>
<script>
import { handleConfirm } from "@/utils/messageBox";
import { userManage } from "@/api/system";
export default {
  props: {
    userForm: {
      type: Object,
      default: () => ({}),
    },
    options: {
      type: Array,
      default: [],
    },
    orgOptions: {
      type: Array,
      default: [],
    },
    organizationId: {
      type: String,
      default: "",
    },
    orgType: {
      type: String,
      default: "",
    },
    visible:{
      type:Boolean,
      default:false,
    },
  },
  data() {
    return {
      loading: false,
      dialogRules: {
        userName: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        password: [
          { required: true, message: "请输入登录密码", trigger: "blur" },
        ],
        loginName: [
          { required: true, message: "请输入登录名", trigger: "blur" },
        ],
        organizationId: [
          { required: true, message: "请选择组织机构", trigger: "change" },
        ],
        roleIds: [
          {
            required: true,
            message: "请选择角色",
            trigger: "change",
            type: "array",
          },
        ],
      },
      currentChooseOrgType: "", //当前选择组织类型
      defaultProps: {
        children: "children",
        label: "name",
        value: "id",
      },
      dialogForm: {
        userName: "",
        loginName: "",
        password: "",
        phone: "",
        roleIds: [],
        organizationId: "",
      },
    };
  },
  watch: {
    visible:{
      handler(val) {
        if(!val){
          this.$refs['dialogForm'].resetFields();
        }
      },
      deep: true,
      immediate: true,
    },
    userForm: {
      handler(val) {
        this.dialogForm = val;
      },
      deep: true, //对象内部的属性监听，也叫深度监听
      immediate: true,
    },
    orgType: {
      handler(val) {
        this.currentChooseOrgType = val;
      },
      deep: true,
      immediate: true,
    },
  },
  created() {},
  methods: {
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.isAdd();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    isAdd() {
      let msg = this.dialogForm.id ? "编辑" : "新增";
      handleConfirm(`确定要${msg}人员信息吗？`, "warning", "提示")
        .then((res) => {
          this.doAdd();
        })
        .catch((err) => {
          // do something cancel
        });
    },
    doAdd() {
      this.loading = true;
      let url = this.dialogForm.id ? "/update" : "/insert";
      let params = { ...this.dialogForm };
      if (this.dialogForm.id) {
        delete params.password;
      }
      userManage(url,'post', {
        ...params,
        type: this.currentChooseOrgType,
      })
        .then((res) => {
          if (res.code == 200) {
            this.$message.success(res.message);
            this.$refs["dialogForm"].resetFields();
            this.$emit("cancelDialog", true);
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    closeModel() {
      this.$emit("cancelDialog", false);
    },
    orgChange(e) {
      let node = this.$refs["orgRef"].getCheckedNodes();
      let datas = node[0].data;
      this.currentChooseOrgType = datas.type;
      this.$set(this.dialogForm, "organizationId", datas.id);

      if (this.currentChooseOrgType == "P") {
        //平台类型
        this.dialogForm.roleIds = this.dialogForm.roleIds.filter(
          (item) => item != 2
        );
      }
    },
  },
};
</script>
<style scoped lang="scss">
.operate-btns {
  text-align: right;
  margin-top: 20px;
}
.el-select {
  width: 100%;
}
::v-deep .el-input.is-disabled .el-input__inner {
  color: #333;
}
</style>
